# -*- coding: utf-8 -*-
from easyxlsx import SimpleWriter
import uuid
import os
# import win32com.client as win32
# import pythoncom
def explode(headers,dataset):
    path = str(uuid.uuid4()) + '.xlsx'
    SimpleWriter(dataset, headers=headers, bookname='./explode/'+path).export() 
    return path

def exchange(filename):
    current_path = os.path.abspath(__file__)
    father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
    fname = father_path+'\\..\\upload\\'+filename
    # return fname
    pythoncom.CoInitialize()
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(fname)
    wb.SaveAs(fname+"x", FileFormat = 51)    #FileFormat = 51 is for .xlsx extension
    wb.Close()                               #FileFormat = 56 is for .xls extension
    excel.Application.Quit()
    return 1
def exchange2(filename):
    current_path = os.path.abspath(__file__)
    father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
    fname = father_path+'\\..\\upload\\'+filename
    # return fname
    pythoncom.CoInitialize()
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(fname)
    # xlsx: FileFormat=51
    # xls:  FileFormat=56
    wb.SaveAs(fname.rstrip('x'), FileFormat = 56)    #FileFormat = 51 is for .xlsx extension
    wb.Close()
    excel.Application.Quit()
    return 1
def exchange3(filename):
    current_path = os.path.abspath(__file__)
    father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
    fname = father_path+'\\..\\upload\\'+filename
    # return fname
    pythoncom.CoInitialize()
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(fname)
    # xlsx: FileFormat=51
    # xls:  FileFormat=56
    wb.SaveAs(fname.replace('csv','xlsx'), FileFormat = 56)    #FileFormat = 51 is for .xlsx extension
    wb.Close()
    excel.Application.Quit()
    return 1
# exchange()
# current_path = os.path.dirname('../upload/2ac6a9eb-b2be-4f3d-aeb2-5bb8f233ef51.xls')
# print(current_path)

# print(current_path)

# exchange(excel)
